



import LocalMap from "../../cfw/local/LocalMap";
import { engine } from "../../engine/engine";
import { WebParam } from "./WebDBDefine";
export let SQL_MAP = 'text'
export let SQL_BOOLEAN = 'integer'

export class WebTable {


    protected _name: string;


    protected _dbName: string;
    protected _db: LocalMap = null;

    protected _params: WebParam[] = [];
    init(db: LocalMap, dbName: string, name: string, params: WebParam[]) {
        this._db = db;
        this._dbName = dbName;
        this._name = name;
        this._params = params;
        this.reset();
    }

    //判断表中是否有这个值
    hasKey(key: string, value: any) {
        let ls = this.list;
        for (let index = 0; index < ls.length; index++) {
            const element = ls[index];
            if (element[key] == value) {
                return true;
            }
        }
        return false;
    }

    isPrimary(key: string) {
        for (let index = 0; index < this._params.length; index++) {
            const element = this._params[index];
            // console.log('isPrimary element ', element, key)
            if (element.key.indexOf(key) >= 0 && element.attr) {
                return true;
            }
        }
        return false;
    }

    delete() {
        this.db.remove(this._name)
    }

    get dbName() {
        return this._dbName;
    }

    get db() {
        return this._db;
    }

    get data() {
        let d = this.db.get(this._name)
        if (!d) {
            engine.error(' table is not have ', this._name)
            d = { head: {}, data: [] }
            this.db.set(this._name, d)
            this.db.update(this._name, d)
        }
        return d;
    }

    private reset() {
        let d = this.db.get(this._name)
        if (!d) {
            this.db.set(this._name, { head: {}, data: [] })
        }
    }

    get hasData() {
        return this.data != undefined;
    }

    get list() {
        return this.data.data;
    }

    update() {
        // console.log(' this.data ', this.data, this._name)
        this.db.update(this._name, this.data)
    }

    get template() {
        let temp = {}
        for (let i = 0; i < this._params.length; i++) {
            const element = this._params[i];
            let key = element.key;
            switch (element.type) {
                case 'text':
                    temp[key] = ''
                    break;
                default:
                    temp[key] = 0;
                    break;
            }
        }
        return temp;
    }

    checkData(data: { [key: string]: any }) {


        for (let i = 0; i < this._params.length; i++) {
            const element = this._params[i];
            let key = element.key;
            let valueType = element.type;
            let value = data[key]
            if (value == undefined) {
                return 'missing key name is ' + key + ' table name is ' + this._name;
            }
            let dataTypeName = typeof (value)
            if (valueType.match(/text/i)) {
                if (dataTypeName != 'string' && dataTypeName != 'number') {
                    return 'type should be string now is   ' + dataTypeName + ' .key is ' + key + '. table name is ' + this._name;
                }
            } else if (valueType.match(/integer/i)) {
                if (dataTypeName == 'string') {
                    return 'type should be integer now is  ' + dataTypeName + '. key is ' + key + '. table name is ' + this._name;
                }
            }
        }
        return ''
    }

    checkDataList(data: { [key: string]: any }[]) {
        if (data.length > 0) {
            return this.checkData(data[0])
        }
        return ''
    }




}